/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import Entity.Address;
import java.util.List;
import javax.ejb.Stateless;
import Entity.SalesLead;
import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.exception.ExistException;
import util.exception.LeadConflictException;
import javax.persistence.Query;

/**
 *
 * @author cheeseng
 */
@Stateless
public class SalesLeadSession implements SalesLeadSessionLocal {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    private EntityManager em;

    @Override
    public Boolean checkConflictLead(String company) throws Exception {
        Query query = em.createQuery("SELECT c FROM SaleLead c WHERE c.company = :company");
        query.setParameter("cperson", company);
        List resultList = query.getResultList();
        if (resultList.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public ArrayList<SalesLead> getAllLeads() {
        Query query = em.createQuery("SELECT l FROM SaleLead l");
        ArrayList<SalesLead> a = new ArrayList<SalesLead>();
        for (Object o : query.getResultList()) {
            SalesLead l = (SalesLead) o;
            a.add(l);
        }
        return a;
    }

    @Override
    public Long createLead(String salutation, String contactPerson, String company, String comAddress1, String comAddress2, String comPostalCode, String comCountry, String phone, String email, String description) throws Exception {

        if (!checkConflictLead(company)) {
            SalesLead l = new SalesLead();
            l.setSalutation(salutation);
            l.setContactPerson(contactPerson);
            l.setCompany(company);
            l.setPhone(phone);
            l.setEmail(email);
            l.setDescription(description);

            Address companyAddress = l.getCompanyAddress();
            companyAddress.setAddress1(comAddress1);
            companyAddress.setAddress2(comAddress2);
            companyAddress.setpostalCode(comPostalCode);
            companyAddress.setCountry(comCountry);
            l.setCompanyAddress(companyAddress);

            em.persist(l);
            em.flush();
            return l.getLeadId();
        }
        throw new Exception("Lead conflict :company" + company);
    }

    @Override
    public boolean removeLead(long LeadId) throws ExistException {
        try {
            SalesLead lead = em.find(SalesLead.class, LeadId);
            if (lead == null)// lead does not exists
            {
                throw new ExistException("LEAD DOES NOT EXIST.");
            } else if (lead.getSaleInquiry().size() > 0) {
                throw new Exception("Sale inquiry exists for this lead. Hence, it cannot be deleted");

            }
            em.remove(lead);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean updateLead(long leadId, String salutation, String contactperson, String phone, String email, String description) throws Exception {
        SalesLead lead = em.find(SalesLead.class, leadId);
        if (lead.equals(null)) {

            throw new Exception("Sale lead record is not found.");
        }
        lead.setSalutation(salutation);
        lead.setContactPerson(contactperson);
        lead.setPhone(phone);
        lead.setEmail(email);
        lead.setDescription(description);

        em.persist(lead);
        em.flush();
        return true;
    }
}
//   public long convertLead(long LeadId, Address billingAddress, Address shippingAddress, String accountName, String customerType, double creditLimit, String creditTerm, Integer creditPeriod, double cashInAdvance) throws ConversionException {
//        throw new UnsupportedOperationException("Not supported yet.");
//   }

